<?php
	require APPPATH .'third_party/admin_controller'.EXT;
	class Members extends Admin_Controller
	{
		public function __construct()
		{
			parent::__construct();
		}
		public function index()
		{
			$this->check_login_admin();
			$data = array();
			$data['result'] = $this->users->list_user();
			$this->load_render($this->load->view('members/list',$data,TRUE));
		}
		public function add()
		{
			$this->check_login_admin();
			$data = array();
			$valid = $this->form_validation;
			$valid->set_rules('txt_fullname','Họ Và Tên','trim|required|xss_clean');
			$valid->set_rules('txt_username','Username','trim|required|xss_clean|min_length[5]|max_length[12]');			
			$valid->set_rules('txt_pass','Password','trim|required|min_length[6]');
			$valid->set_rules('txt_pass2','Password Confirmation','trim|required|matches[txt_pass]');
			$valid->set_rules('txt_date','Ngày sinh','trim|required|xss_clean');
			$valid->set_rules('txt_phone','Điện thoại','trim|required|xss_clean|numeric');
			$valid->set_rules('txt_address','Địa chỉ','trim|required|xss_clean');			
			$valid->set_rules('txt_gender','Giới tính','trim|required|xss_clean');
			$valid->set_rules('txt_city','Thành phố','trim|required|xss_clean');
			$valid->set_rules('txt_level','Phân Quyền','trim|required|xss_clean');
			$valid->set_rules('txt_yahoo','Yahoo','trim|required|xss_clean');
			$valid->set_rules('txt_skype','Skype','trim|required|xss_clean');
			$valid->set_rules('txt_email','Email','trim|required|xss_clean|valid_email');
			$image = $this->input->post('image');
			$file = isset($_FILES['txt_file']) ? $_FILES['txt_file'] : "" ;
			$uid = $this->input->post('txt_uid');
			$id = $this->uri->segment('4');
			if($valid->run() == TRUE)
			{
				
				$data = array(
					 'fullname' =>$this->input->post('txt_fullname'),
					'username' =>$this->input->post('txt_username'),					
					'phone'		=>$this->input->post('txt_phone'),
					'address'	=>$this->input->post('txt_address'),
					'info'		=>$this->input->post('txt_info',true),
					'gender'	=>$this->input->post('txt_gender'),
					'level'	=>$this->input->post('txt_level'),					
					'city'	    =>$this->input->post('txt_city'),	
					'email'	    =>$this->input->post('txt_email'),	
					'birthday'	=>$this->input->post('txt_date'),
					'yahoo'	=>$this->input->post('txt_yahoo'),
					'skype'	=>$this->input->post('txt_skype'),
					'key'		=> random_string('alnum', 40)					
				);
				
				 if($file !='')
					{
							$config['upload_path'] = './upload/avarta/';
							$config['allowed_types'] = 'gif|jpg|png';
							$config['remove_spaces'] = TRUE;
							$config['max_size'] = 1024;
							$config['max_width'] = '1024';
							$config['max_height'] = '1024';
							$config['file_name'] = $_FILES['txt_file']['name'];
							$this->load->library('upload', $config);
							if(!$this->upload->do_upload('txt_file')) 
							{
							return false;
							} else {
							$upload = $this->upload->data();
							$data['image'] = 'upload/avarta/'.$upload['file_name'];
							}                    
					}
					else
					{
						if($image !='') {
								$data['image'] = $image;								
								} else {
									$data['image'] = '';
								
								}
					}
					
					if($uid=='')
					{
						if($this->users->create_users($data))
						{
							$data['error'] = "Thanh cong";
							redirect('admin/members');
						} else {
							$data['error'] = "khong Thanh cong";	
						}
					}
								
			}
			else
			{
				$data['error'] = "Bạn chưa điền thông tin đầy đủ";
			}
			
			$data['result'] = $this->users->get_id_members($id);		        
			$this->load_render($this->load->view('members/add',$data,TRUE));
		}
		public function update_members()
		{
			$this->check_login_admin();
			$data = array();	
			$valid = $this->form_validation;		
			$valid->set_rules('txt_fullname','Họ Và Tên','trim|required|xss_clean');
			$valid->set_rules('txt_username','Username','trim|required|xss_clean|min_length[5]|max_length[12]');			
			$valid->set_rules('txt_date','Ngày sinh','trim|required|xss_clean');
			$valid->set_rules('txt_phone','Điện thoại','trim|required|xss_clean|numeric');
			$valid->set_rules('txt_address','Địa chỉ','trim|required|xss_clean');			
			$valid->set_rules('txt_gender','Giới tính','trim|required|xss_clean');
			$valid->set_rules('txt_city','Thành phố','trim|required|xss_clean');
			$valid->set_rules('txt_level','Phân Quyền','trim|required|xss_clean');
			$valid->set_rules('txt_yahoo','Yahoo','trim|required|xss_clean');
			$valid->set_rules('txt_skype','Skype','trim|required|xss_clean');
			$valid->set_rules('txt_email','Email','trim|required|xss_clean|valid_email');
			$image = $this->input->post('image');
			$file = isset($_FILES['txt_file']) ? $_FILES['txt_file'] : "" ;
			$uid = $this->input->post('txt_uid');
			$id = $this->uri->segment('4');
			if($valid->run() == TRUE)
			{
				
				$data = array(
					 'fullname' =>$this->input->post('txt_fullname'),
					'username' =>$this->input->post('txt_username'),					
					'phone'		=>$this->input->post('txt_phone'),
					'address'	=>$this->input->post('txt_address'),
					'info'		=>$this->input->post('txt_info',true),
					'gender'	=>$this->input->post('txt_gender'),
					'level'	=>$this->input->post('txt_level'),					
					'city'	    =>$this->input->post('txt_city'),	
					'email'	    =>$this->input->post('txt_email'),	
					'birthday'	=>$this->input->post('txt_date'),
					'yahoo'	=>$this->input->post('txt_yahoo'),
					'skype'	=>$this->input->post('txt_skype'),
													
				);
				 
				 if($file !='')
					{
							$config['upload_path'] = './upload/avarta/';
							$config['allowed_types'] = 'gif|jpg|png';
							$config['remove_spaces'] = TRUE;
							$config['max_size'] = 1024;
							$config['max_width'] = '1024';
							$config['max_height'] = '1024';
							$config['file_name'] = $_FILES['txt_file']['name'];
							$this->load->library('upload', $config);
							if(!$this->upload->do_upload('txt_file')) 
							{
							return false;
							} else {
							$upload = $this->upload->data();
							$data['image'] = 'upload/avarta/'.$upload['file_name'];
							}       
							        
					}
					else
					{
						if($image !='') {
								$data['image'] = $image;								
								} else {
									$data['image'] = '';
								
								}
					}
					if($this->users->update_users($uid,$data))
						{
							$data['error'] = "Thanh cong";
							redirect('admin/members');
							
							
						} else {
							$data['error'] = "khong Thanh cong";	
						}    
					
						
						
								
					
			}
			else
			{
				$data['error'] = "Bạn chưa điền thông tin đầy đủ";
			}
			 $data['uid']  = $id;
			 $data['list_country'] = $this->mains->list_country();	        
			$data['result'] = $this->users->get_id_members($id);
			$this->load_render($this->load->view('members/update_info',$data,TRUE));
		}
		public function delete_memebers()
		{
			
		}
		public function ajax_upload()
		{
			$data = array();
			$this->load_render($this->load->view('ajax/upload',$data,TRUE));
		}
	}
?>